package com.spring.sunflower.common;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.spring.sunflower.R;
import com.spring.sunflower.anchor.PersonalDetailActivity;
import com.spring.sunflower.base.BaseFragment;
import com.spring.sunflower.bean.FansBean;
import com.spring.sunflower.constant.Constant;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kevin on 2021/7/16<br/>
 * <p>
 * Describe:<br/>
 */
public class FansFragment extends BaseFragment<RelationPresenter> implements CommonContract.RelationView, OnRefreshListener, OnLoadMoreListener {
    private SmartRefreshLayout refreshLayout;
    private RecyclerView mRecyclerView;
    private int pageIndex = 1;
    private FansAdapter mAdapter;
    private List<FansBean.DatasBean> mData = new ArrayList<>();
    private String userID;
    private String type = "2";
    private boolean hasFollowed = false;
    private int clickPos = -1;
    private LinearLayout llEmpty;

    @Override
    public int setLayoutResId() {
        return R.layout.fragment_fans;
    }

    @Override
    public void initView() {
        refreshLayout = mView.findViewById(R.id.refreshLayout);
        mRecyclerView = mView.findViewById(R.id.rvRecyclerView);
        llEmpty = mView.findViewById(R.id.llEmpty);
        mAdapter = new FansAdapter(R.layout.adapter_item_fans, mData);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
        userID = getField(Constant.KEY_USERID, "");
        mAdapter.addChildClickViewIds(R.id.ivCover, R.id.btnAction);
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                FansBean.DatasBean datasBean = mData.get(position);
                switch (view.getId()) {
                    case R.id.ivCover:
                        Intent intent = new Intent(getActivity(), PersonalDetailActivity.class);
                        intent.putExtra(Constant.KEY_TARGET_ID, String.valueOf(datasBean.getMemberId()));
                        startActivity(intent);
                        break;
                    case R.id.btnAction:
                        clickPos = position;
                        if (datasBean.getIsFavorite().equals("1")) {//关注了对方，点击事件会取消关注
                            hasFollowed = true;
                            mPresenter.setFollowed(datasBean.getMemberId(), "0");
                        } else {//未关注对方，点击事件会添加关注
                            hasFollowed = false;
                            mPresenter.setFollowed(datasBean.getMemberId(), "1");
                        }
                        break;
                }
            }
        });
    }

    @Override
    public void initData() {
        mPresenter.getFansOrFollowed(userID, String.valueOf(pageIndex), type);

    }

    @Override
    public void initListener() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);

    }

    @Override
    public void loadData() {

    }

    @Override
    public RelationPresenter setPresenter() {
        return new RelationPresenter(this);
    }

    @Override
    public void showRelationList(List<FansBean.DatasBean> data) {
        for (FansBean.DatasBean bean : data) {
            bean.setFans(true);
        }
        if (pageIndex == 1) {
            if (data != null && data.size() > 0) {
                llEmpty.setVisibility(View.GONE);
                refreshLayout.setVisibility(View.VISIBLE);
                mAdapter.setList(data);
            } else {
                llEmpty.setVisibility(View.VISIBLE);
                refreshLayout.setVisibility(View.GONE);
            }
        } else {
            mAdapter.addData(data);
        }
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh();
        }
        if (refreshLayout.isLoading()) {
            refreshLayout.finishLoadMore();
        }

    }

    @Override
    public void setFollowedSuccess() {
        FansBean.DatasBean datasBean = mData.get(clickPos);
        if (hasFollowed) {
            datasBean.setIsFavorite("0");
        } else {
            datasBean.setIsFavorite("1");
        }
        mAdapter.notifyDataSetChanged();
        clickPos = -1;
    }

    @Override
    public void setFollowedFailed() {
        clickPos = -1;

    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        pageIndex = 1;
        mPresenter.getFansOrFollowed(userID, String.valueOf(pageIndex), type);
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        mPresenter.getFansOrFollowed(userID, String.valueOf(++pageIndex), type);
    }
}
